using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using NCT.Framework;
using NCT.Framework.Database;

namespace VISS.API.Events {

    public class NoelEventBC {

        public static bool Insert(NoelEventEnt item) {
            bool result = true;
            DbConnection dbCon = null;
            DbTransaction dbTrans = null;
            try {
                //Get connection name
                string connectionName = ConfigInfo.ConnectionName;

                //Create connection & transaction
                dbCon = Database.CreateConnection(connectionName, true);
                dbTrans = dbCon.BeginTransaction();

                result = NoelEventDA.Insert(item, connectionName, dbCon, dbTrans) > 0;
            }
            catch (System.Exception ex) {
                LogUtils.Error(string.Format("VISS.Events.NoelEvent.Insert({0}): \n" + ex.ToString(), SerializerUtils.JsonSerialize(item)));
                result = false;
            }
            finally {
                if (dbTrans != null) {
                    if (result) {
                        dbTrans.Commit();
                    }
                    else {
                        // rollback
                        dbTrans.Rollback();
                    }
                }
                if (dbCon != null && dbCon.State == ConnectionState.Open) {
                    dbCon.Close();
                }
            }
            return result;
        }

        public static bool Update(NoelEventEnt item) {
            bool result = true;
            DbConnection dbCon = null;
            DbTransaction dbTrans = null;
            try {
                //Get connection name
                string connectionName = ConfigInfo.ConnectionName;

                //Create connection & transaction
                dbCon = Database.CreateConnection(connectionName, true);
                dbTrans = dbCon.BeginTransaction();

                result = NoelEventDA.Update(item, connectionName, dbCon, dbTrans) > 0;
            }
            catch (System.Exception ex) {
                LogUtils.Error(string.Format("VISS.Events.NoelEvent.Update({0}): \n" + ex.ToString(), SerializerUtils.JsonSerialize(item)));
                result = false;
            }
            finally {
                if (dbTrans != null) {
                    if (result) {
                        dbTrans.Commit();
                    }
                    else {
                        // rollback
                        dbTrans.Rollback();
                    }
                }
                if (dbCon != null && dbCon.State == ConnectionState.Open) {
                    dbCon.Close();
                }
            }
            return result;
        }

        public static NoelEventEnt GetByUserKey(string userKey) {
            NoelEventEnt result = null;
            try {
                //Get connection name
                string connectionName = ConfigInfo.ConnectionName;

                result = NoelEventDA.GetByUserKey(userKey, connectionName, null, null);
            }
            catch (System.Exception ex) {
                LogUtils.Error(string.Format("VISS.Events.NoelEvent.GetByUserKey({0}): \n" + ex.ToString(), userKey));
            }

            return result;
        }
    }
}